﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

using DbSharp.Framework.DbObject;


namespace DbSharp.Framework.CaseFile
{

    /// <summary>
    /// Resultset output setting class
    /// </summary>
    class ResultsetOutputSetting : IResultsetOutputSetting
    {
       
        #region Private members

        DefaultRsOutputSetting m_DefaultOutputSetting;

        Dictionary<string, string> m_ListOutputFieldNamePair = new Dictionary<string, string>();

        Dictionary<string, string> m_ListFieldNullPlaceholderPair = new Dictionary<string, string>();

        string m_OutputArgument;

        Nullable<long> m_ExportRecordCount;

        private bool m_WrapStringByCDATA;
        #endregion



        public ResultsetOutputSetting(DefaultRsOutputSetting defaultOutputSetting, string OutputArgument)
        {
            m_DefaultOutputSetting = defaultOutputSetting; 
            if (OutputArgument.Trim().Length==0)
                throw new ArgumentException("OutputArgument must not be empty.");

            m_OutputArgument = OutputArgument.Trim().ToUpper(); 
        }


        public string GetFieldOutputName(string fieldName)
        {
            if (string.IsNullOrEmpty(fieldName))
                return string.Empty;


            if (m_ListOutputFieldNamePair.Count == 0)
            {
                return m_DefaultOutputSetting.GetFieldOutputName(fieldName);
            }
            else
            {
                var upperName = fieldName.Trim().ToUpper();
                if (m_ListOutputFieldNamePair.ContainsKey(upperName))
                    return m_ListOutputFieldNamePair[upperName];
                else
                    return string.Empty;
            }
        }


        #region IResultsetOutputSetting Members

        public bool IndicateOutputAllFields
        {
            get
            {
                //if you specify some outputFields, this resultset will only output these fields rather than all fields.
                if (m_ListOutputFieldNamePair.Count == 0)
                    return true;
                else
                    return false;
            }
        }

         
        public bool WrapStringByCDATA
        {
            get { return m_WrapStringByCDATA; }
            set { m_WrapStringByCDATA = value; }
        }


        public string OutputArgument
        {
            get { return m_OutputArgument; }
        }


        public bool OutputAllOutArguments
        {
            get { return false; }
        }

           
        public long ExportRecordCount 
        {
            get
            {
                if (m_ExportRecordCount==null)
                    return m_DefaultOutputSetting.ExportRecordCount;
                else
                    return (long)m_ExportRecordCount;
            }
            set
            {
                if (m_ExportRecordCount < 0)
                    m_ExportRecordCount = long.MaxValue;
                else
                    m_ExportRecordCount = value;
            }
        }
     


        public void AddOutputFieldName(string fieldName, string outputName, string nullValuePlaceholder)
        {
            if (!m_ListOutputFieldNamePair.ContainsKey(fieldName))
            {
                if (fieldName.Trim().Length == 0)
                    throw new ArgumentException("fieldName cannot is an empty string.");
                
                //save outputName
                if (outputName.Trim().Length == 0)
                    outputName = fieldName;
                m_ListOutputFieldNamePair.Add(fieldName.Trim().ToUpper(), outputName.Trim());

                //save nullValuePlaceHolder
                if (string.IsNullOrEmpty(nullValuePlaceholder))
                    nullValuePlaceholder = string.Empty;
                m_ListFieldNullPlaceholderPair.Add(fieldName.Trim().ToUpper(), nullValuePlaceholder);
            }
        }


        /// <summary>
        /// key: UpperCase fieldName 
        /// value: outputFieldName
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetFieldOutputNamePairs()
        {
            return m_ListOutputFieldNamePair;
        }



        /// <summary>
        /// key: UpperCase fieldName
        /// value: fieldNullValuePlaceHolder
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetFieldNullPlaceholderPairs()
        {
            return m_ListFieldNullPlaceholderPair;
        }

        #endregion
    }
    
}
